################################################
#Code to reproduce Figure OB6 [Value Coefficients without Belgian rspond]
################################################

library(rio)
library(tidyverse)
library(ggstance)

#####################
#Functions
####################

`%notin%` <- Negate(`%in%`)

m1_func <- function(x) {
  x %>%
    filter(parm %notin% c("_cons", "1.post_soviet", "demyrs_01", "ethnic_01")) %>%
    filter(!is.na(z)) %>%
    mutate(model = "Model 1")
}

m2_func <- function(x) {
  x %>%
    filter(parm %notin% c("_cons", "1.post_soviet", "demyrs_01", "ethnic_01")) %>%
    filter(!is.na(z)) %>%
    mutate(model = "Model 2")
}

m3_func <- function(x) {
  x %>%
    filter(parm %notin% c("_cons", "1.post_soviet", "demyrs_01", "ethnic_01")) %>%
    filter(!is.na(z)) %>%
    mutate(model = "Model 3")
}

#####################
#Vote
####################

v1 <- import("vote_elections1_nobelgium.dta") %>% m1_func()
v2 <- import("vote_elections2_nobelgium.dta") %>% m2_func()
v3 <- import("vote_elections3_nobelgium.dta") %>% m3_func()

#####################
#Support Others
####################
s1 <- import("support_others1_nobelgium.dta") %>% m1_func()
s2 <- import("support_others2_nobelgium.dta") %>% m2_func()
s3 <- import("support_others3_nobelgium.dta") %>% m3_func()

#####################
#Obey
####################
o1 <- import("obey1_nobelgium.dta") %>% m1_func()
o2 <- import("obey2_nobelgium.dta") %>% m2_func()
o3 <- import("obey3_nobelgium.dta") %>% m3_func()

#####################
#Opinions
####################
op1 <- import("opinions1_nobelgium.dta") %>% m1_func()
op2 <- import("opinions2_nobelgium.dta") %>% m2_func()
op3 <- import("opinions3_nobelgium.dta") %>% m3_func()

#####################
#Volunteer
####################

vo1 <- import("volunteer1_nobelgium.dta") %>% m1_func()
vo2 <- import("volunteer2_nobelgium.dta") %>% m2_func()
vo3 <- import("volunteer3_nobelgium.dta") %>% m3_func()

#####################
#Be Active in Politics
####################

a1 <- import("beactive1_nobelgium.dta") %>% m1_func()
a2 <- import("beactive2_nobelgium.dta") %>% m2_func()
a3 <- import("beactive3_nobelgium.dta") %>% m3_func()

#####################
#Combine (Export)
####################

comb <- bind_rows(v1, v2, v3, 
                  a1, a2, a3, 
                  o1, o2, o3, 
                  op1, op2, op3, 
                  s1, s2, s3,
                  vo1, vo2, vo3)



#####################
#Figure
####################

comb1 <- comb %>%
  filter(parm %in% c("self_te", "cons_open"))

comb1a <- comb1 %>%
  mutate(label = factor(label, 
                        levels=c("Self Transcendence vs. Self-Enhancement", 
                                 "Conservation vs. Openness")), 
         model = factor(model, 
                        levels=c("Model 3", "Model 2", "Model 1")), 
         eq = factor(eq, 
                     levels=c("vote_elections", "beactive", "volunteer", 
                              "obey", "opinions", "support_others"), 
                     labels=c("Vote", "Be Active", "Volunteer", 
                              "Obey", "Opinions", "Support Others")))

comb1a %>%
  ggplot(aes(x=estimate, y=model)) + 
  geom_pointrangeh(aes(xmin=min95, xmax=max95)) + 
  facet_grid(eq ~ label, labeller = label_wrap_gen(width = 25)) + 
  geom_vline(xintercept=0, linetype="dashed", color="red") + 
  labs(y=NULL, x = "Coefficient") + 
  theme_bw(14) + 
  theme(strip.text.y = element_text(angle=0), 
        axis.title.y = element_text(margin = margin(r=15)), 
        strip.text = element_text(face = "bold")) + 
  labs(title = "No Belgian Respondents")

#With labels
comb1a %>%
  ggplot(aes(x=estimate, y=model)) + 
  geom_pointrangeh(aes(xmin=min95, xmax=max95)) + 
  facet_grid(eq ~ label, labeller = label_wrap_gen(width = 25)) + 
  geom_vline(xintercept=0, linetype="dashed", color="red") + 
  labs(y=NULL, x = "Coefficient") + 
  geom_text(aes(label=round(estimate, 2)), 
            nudge_y=0.3) +
  theme_bw(14) + 
  theme(strip.text.y = element_text(angle=0), 
        axis.title.y = element_text(margin = margin(r=15)), 
        strip.text = element_text(face = "bold")) + 
  labs(title = "No Belgian Respondents")
  

ggsave("figure1_labels_nobelgium.png", height=10, width=12, dpi=600)


